
import 'package:flutter/material.dart';

import '../generated/l10n.dart';

class _DialogContainer extends StatelessWidget {
  final String titleText;
  final String subTitleText;
  final String confirmText;
  final String cancelText;
  final Widget content;
  final bool enableCancel;
  final bool enableActions;
  final VoidCallback? onConfirm;
  final VoidCallback? onCancel;

  _DialogContainer(
      {this.titleText = '',
        this.enableCancel = false,
        this.enableActions = true,
        this.onConfirm,
        this.onCancel,
        this.subTitleText = '',
        this.confirmText = '',
        this.cancelText = '',
        required this.content});

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    return AlertDialog(
      insetPadding:
      const EdgeInsets.symmetric(vertical: 20.0, horizontal: 20.0),
      title: SizedBox(
        width: MediaQuery.of(context).size.width * 0.8,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Visibility(
                visible: titleText != '',
                child: Text(titleText, style: TextStyle(fontSize: 18.0))),
            SizedBox(
              height: 8.0,
            ),
            Visibility(
                visible: subTitleText != '',
                child: Text(
                  subTitleText,
                  style: themeData.textTheme.subtitle2
                      ?.copyWith(color: Colors.grey),
                ))
          ],
        ),
      ),
      semanticLabel: '',
      content: content,
      actions: enableActions
          ? [
        Visibility(
            visible: enableCancel,
            child: TextButton(
              onPressed: () => onCancel == null
                  ? Navigator.of(context).pop()
                  : onCancel?.call(),
              child: Text(cancelText == ''
                  ? 'Cancel'
                  : cancelText),
            )),
        TextButton(
            onPressed: () => onConfirm?.call(),
            child: Text(confirmText == ''
                ? 'Confirm'
                : confirmText))
      ]
          : null,
    );
  }
}


class ZenggeTextAlertDialog extends StatelessWidget {
  final String titleText;
  final String subTitleText;
  final bool enableCancel;
  final String contentText;
  final String textButtonText;
  final String confirmText;
  final String cancelText;
  final bool enableActions;
  final VoidCallback? onTextButtonClick;

  ZenggeTextAlertDialog(this.contentText,
      {this.titleText = '',
        this.subTitleText = '',
        this.confirmText = '',
        this.cancelText = '',
        this.textButtonText = '',
        this.enableCancel = false,
        this.enableActions = true,
        this.onTextButtonClick});

  @override
  Widget build(BuildContext context) {
    return _DialogContainer(
        titleText: titleText,
        subTitleText: subTitleText,
        enableActions: enableActions,
        enableCancel: enableCancel,
        confirmText: confirmText,
        cancelText: cancelText,
        onConfirm: () => Navigator.of(context).pop(true),
        onCancel: () => Navigator.of(context).pop(false),
        content: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                contentText,
                style: TextStyle(color: Colors.grey),
              ),
              Visibility(
                  visible: onTextButtonClick != null,
                  child: TextButton(
                    onPressed: () => onTextButtonClick?.call(),
                    child: Text(textButtonText),
                  ))
            ],
          ),
        ));
  }
}

class ZenggeInputAlertDialog extends StatefulWidget {
  final String titleText;
  final String subTitleText;
  final String initText;
  final String? hintText;
  final bool enableCancel;
  final int? maxLength;
  final TextInputType? keyboardType;
  final bool enableInputEmpty;

  ZenggeInputAlertDialog(
      {required this.titleText,
        this.maxLength,
        this.keyboardType,
        this.initText = '',
        this.subTitleText = '',
        this.hintText,
        this.enableCancel = false,
        this.enableInputEmpty = false });

  @override
  State<StatefulWidget> createState() {
    return _ZenggeInputAlertDialogState();
  }
}

class _ZenggeInputAlertDialogState extends State<ZenggeInputAlertDialog> {
  late TextEditingController controller;
  String? errorText;

  @override
  void initState() {
    controller = TextEditingController(text: widget.initText);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _DialogContainer(
          titleText: widget.titleText,
          subTitleText: widget.subTitleText,
          enableCancel: widget.enableCancel,
          onConfirm: () {
            if (!widget.enableInputEmpty && controller.text.trim().isEmpty) {
              setState(() {
                errorText = 'Please enter content';
              });
              return;
            }
            Navigator.of(context).pop(controller.text);
          },
          // onCancel: (){
          //   Navigator.of(context).pop();
          // },
          content: TextField(
            autofocus: true,
            controller: controller,
            maxLength: widget.maxLength,
            keyboardType: widget.keyboardType,
            // inputFormatters: [
            //   RegexFormatter(regex: RegexUtil.regexFirstNotNull),
            // ],
            style: TextStyle(
                color: Colors.grey),
            onChanged: (String text) {
              if (widget.maxLength != null &&
                  text.length >= widget.maxLength!) {
                setState(() {
                  errorText = 'The maximum number of characters entered has been reached';
                });
              } else {
                if (errorText != null) {
                  setState(() {
                    errorText = null;
                  });
                }
              }
            },
            decoration: InputDecoration(
              counterText: '',
              errorText: errorText,
              hintText: widget.hintText,
            ),
          )),
    );
  }
}